/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.gov.mtc.dgac.licencias.persistence;

import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import pe.gov.mtc.dgac.licencias.bean.SacpTO;
import pe.gov.mtc.dgac.licencias.util.Utiles;

/**
 *
 * @author elvis
 */
@Repository
public class SacpHibDAO extends HibernateDaoSupport implements SacpDAO{

     private final static Logger logger = Logger.getLogger(SacpHibDAO.class);
    
    @Autowired
    public SacpHibDAO(SessionFactory sessionFactory){
         setHibernateTemplate( new HibernateTemplate(sessionFactory));
    }
    
    public List<SacpTO> listarSaco(SacpTO sacp) {
         DetachedCriteria criteria = DetachedCriteria.forClass(SacpTO.class);
        logger.debug("listarSaco()");
        if( !Utiles.nullToBlank( sacp.getCodHallazgo() ).equals("")){
             criteria.add( Restrictions.eq("codHallazgo.codHallazgo", sacp.getCodHallazgo().getCodHallazgo().intValue())) ;
        }        
        return getHibernateTemplate().findByCriteria(criteria);
    }

    public void registrarSacp(SacpTO sacp) {
        this.getHibernateTemplate().saveOrUpdate(sacp);
    }

    public void eliminarSacp(int id_sacp) {
        getSession().createQuery(" delete from  SacpTO where codSacp = :id ")
        .setInteger("id", id_sacp)
        .executeUpdate();
    }

    public SacpTO obtenerSacp(int id_sacp) {
         Query query = getSession().createQuery(" from SacpTO a where codSacp = :id ")
        .setInteger("id", id_sacp);
        return (SacpTO) query.uniqueResult();
    }

}
